package com.meishij.fragment;


import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RadioGroup;

import com.chanven.lib.cptr.PtrFrameLayout;
import com.meishij.R;
import com.meishij.adapter.RecommendRecyclerViewAdapter;
import com.meishij.entry.RecommendEntry;
import com.meishij.server.RecommendServer;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;


/**
 * A simple {@link Fragment} subclass.
 */
public class RecommendFragment extends Fragment implements View.OnClickListener {


    private RecyclerView recommend_recyclerView;
    private Context context;
    private FragmentManager childFragmentManager;
    private View recommend_top;
    private ImageView recommend_top_image_left;
    private RecommendEntry recommendEntry;
    private PtrFrameLayout recommend_ptrframelayout;

    private RadioGroup radioGroup;
    private int pos;

    public RecommendFragment() {

    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        context = container.getContext();
//        childFragmentManager = getChildFragmentManager();
        childFragmentManager = getFragmentManager();
//        Bundle bundle = getArguments();
//        recommendEntry = (RecommendEntry) bundle.getSerializable("responseData");
        return inflater.inflate(R.layout.fragment_recommend, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        recommend_recyclerView = ((RecyclerView) view.findViewById(R.id.recommend_recyclerView));
        recommend_recyclerView.setLayoutManager(new LinearLayoutManager(context));
        recommend_top = view.findViewById(R.id.recommend_top);
        recommend_top_image_left = ((ImageView) recommend_top.findViewById(R.id.recommend_top_image_left));
        recommend_top_image_left.setOnClickListener(this);

        Retrofit retrofit = new Retrofit
                .Builder()
                .baseUrl("http://api.meishi.cc/v5/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        RecommendServer recommendServer = retrofit.create(RecommendServer.class);
        Call<RecommendEntry> recommend = recommendServer.getRecommendEntry();
        recommend.enqueue(new Callback<RecommendEntry>() {
            @Override
            public void onResponse(Call<RecommendEntry> call, Response<RecommendEntry> response) {
                recommendEntry = response.body();
                if (recommendEntry != null) {
                    recommend_recyclerView.setAdapter(new RecommendRecyclerViewAdapter(childFragmentManager, recommendEntry.getObj()));
                }
            }

            @Override
            public void onFailure(Call<RecommendEntry> call, Throwable t) {
            }
        });


        //recommend_ptrframelayout = ((PtrFrameLayout) view.findViewById(R.id.recommend_ptrframelayout));
        /*TextView textview = new TextView(context);
        textview.setText("刷新中。。。");
        textview.setTextColor(Color.RED);
        recommend_ptrframelayout.setHeaderView(textview);
//        final StoreHouseHeader header = new StoreHouseHeader(getContext());
//        header.setPadding(0, LocalDisplay.dp2px(15), 0, 0);
//        header.initWithString("Alibaba");
//        header.initWithStringArray(R.array.storehouse);
        recommend_ptrframelayout.setPtrHandler(new PtrHandler() {
            @Override
            public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
                return  PtrDefaultHandler.checkContentCanBePulledDown(frame, content, header);
            }

            
            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {

                frame.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        recommend_ptrframelayout.refreshComplete();
                    }
                }, 1800);
            }
        });*/


    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.recommend_top_image_left:
                PopupWindow popupWindow = new PopupWindow(context);
                View view = LayoutInflater.from(context).inflate(R.layout.fragment_recommend_top_popupwindow, null);
                popupWindow.setContentView(view);
                popupWindow.setFocusable(true);
                popupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
                popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
                popupWindow.showAsDropDown(v);
                popupWindow.setTouchable(true);
                popupWindow.setOutsideTouchable(true);
//                popupWindow.setBackgroundDrawable(new BitmapDrawable(getResources(), (Bitmap) null));
                popupWindow.setBackgroundDrawable(null);
                break;
        }

    }
}
